package com.puff.controller.platform;

import com.puff.constant.JwtClaimsConstant;
import com.puff.constant.RoleConstant;
import com.puff.dto.AdminEditPasswordDTO;
import com.puff.dto.AdminLoginDTO;
import com.puff.dto.EmplyeeEditPasswordDTO;
import com.puff.entity.Admin;
import com.puff.properties.JwtProperties;
import com.puff.result.Result;
import com.puff.service.AdminService;
import com.puff.utils.JwtUtil;
import com.puff.vo.AdminLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 平台管理员相关接口
 */
@RestController
@RequestMapping("/platform/admin")
@Slf4j
@Api(tags = "平台管理员接口")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 平台管理员登录
     * @param adminLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value = "平台管理员登录")
    public Result<AdminLoginVO> login(@RequestBody AdminLoginDTO adminLoginDTO) {
        log.info("平台管理员登录：{}", adminLoginDTO);

        Admin admin = adminService.login(adminLoginDTO);

        //登录成功后，生成jwt令牌
        // jwt令牌用于加密或验证，后期还需要用该jwt令牌来验证
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.PLAT_ID, admin.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getPlatformSecretKey(),
                jwtProperties.getPlatformTtl(),
                claims);

        AdminLoginVO adminLoginVO = AdminLoginVO.builder()
                .id(admin.getId())
                .username(admin.getUsername())
                .name(admin.getName())
                .token(token)
                .Role(RoleConstant.PLATFORM_SIDE)
                .build();

        return Result.success(adminLoginVO);
    }

    /**
     * 平台管理员退出登录
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation(value = "平台管理员退出登录")
    public Result<String> logout() {
        log.info("平台管理员退出登录......");
        return Result.success();
    }

    /**
     * 修改密码
     * @param adminEditPasswordDTO
     * @return
     */
    @PutMapping("/editPassword")
    @ApiOperation("修改密码")
    public Result editPassword(@RequestBody AdminEditPasswordDTO adminEditPasswordDTO){
        log.info("修改密码:{}",adminEditPasswordDTO);
        adminService.editPassword(adminEditPasswordDTO);
        return Result.success();
    }
}
